<?php
require_once($application_folder."/controllers/navigator.php");
class Cms extends navigator 
{

    function __construct()
    {
        parent::__construct();

        $this->layout->setLayout('layout_cms'); 
        $this->lang->load('jadroagent', $this->lang_ses->getlang());

    }

    function index( )
    { 


        if($this->session->userdata('logged_in')){
            $this->text_action();  
        }else{
            $this->navigate_cms('cms/loginform');   
            return;
        }

    }

    function text_action(){


        $this->navigate_cms('cms/home'); 

    }

    function view_text(){

        $page_id = $_POST['page_id'];

        $myar = explode("-", $page_id);

        if(!isset($myar[1])) $myar[1] = '';
        if(!isset($myar[2])) $myar[2] = '';

        $row = $this->db->get_where('text',array('page'=>$myar[0],'sub'=>$myar[1],'upsub'=>$myar[2],'lang' => $this->lang_ses->getLang()))->result_array();

        echo json_encode(array('row'=>$row));


    }

    function edit_text(){

        $action = FALSE;
        $msg = '';

        if($this->validation()){
            $action = TRUE;


            $page_id = $_POST['page_id'];

            $myar = explode("-", $page_id);

            if(!isset($myar[1])) $myar[1] = '';
            if(!isset($myar[2])) $myar[2] = '';


            $row = $this->db->get_where('text',array('page'=>$myar[0],'sub'=>$myar[1],'upsub'=>$myar[2],'lang' => $this->lang_ses->getLang()))->row();

            if(count($row)==1){

                $this->db->where('id', $row->id);
                $this->db->update('text', array('title' => $_POST['title'], 'text' => $_POST['text']));

            }else if(count($row)==0){

                    $newrow = array(
                    'page'=>$myar[0],
                    'sub'=>$myar[1],
                    'upsub'=>$myar[2],
                    'lang'=>$this->lang_ses->getLang(),
                    'title'=>$_POST['title'],     
                    'text'=>$_POST['text'] 
                    );

                    $this->db->insert('text', $newrow);

                }else{
                    $action = FALSE;
                    $msg = 'Unknow error.<br />Row count >1';
            }


        }else{

            $msg = validation_errors();

        }

        echo json_encode(array('action' => $action, 'msg' => $msg)); 


    }

    function validation(){

        $this->load->library('form_validation');

        $this->form_validation->set_rules('page_id', $this->lang->line('page'), 'required|check_dropdown');
        $this->form_validation->set_rules('title', $this->lang->line('title'), 'required');
        $this->form_validation->set_rules('text', $this->lang->line('text'), 'required');


        $this->form_validation->set_message('required',$this->lang->line('enter').' <b>%s</b>!');
        $this->form_validation->set_message('check_dropdown', $this->lang->line('choose').' <b>%s</b>!');

        if ($this->form_validation->run() == FALSE)
        {                
            return FALSE;
        }
        else
        {
            return TRUE;
        }

    }

    function language($lng){

        $this->lang_ses->setLang($lng);
        redirect( base_url().'cms' );

    }



}